<?php 

class giftModel extends appModel
{
    
    /**
     * 修改游戏配置信息
     * @param unknown $info
     * @param unknown $id
     * @return Ambigous <boolean, resource>
     */
    public function updateConfig($info, $id)
    {
        $data = yf_set_update($info);
        $sql = " UPDATE ".$this->table('gift_config')." SET ".$data." WHERE id='".$id."' ";
        return $this->query($sql);
    }
    
    /**
     * 获取参与记录
     * @param unknown $page
     * @param number $limit
     * @return Ambigous <string, multitype:string Ambigous <boolean, multitype:multitype: > >
     */
    public function getJoinLog($page, $limit = 20)
    {
        $sql = " SELECT * FROM ".$this->table('gift_join')." ORDER BY allScore DESC ";
        $data = $this->show_page($sql, $page, $limit);
        if($data['result'])
        {
            $num = ($page - 1) * $limit + 1;
            foreach($data['result'] as $k => $v)
            {
                $tempUser = $this->getUserInfo($v['wxid'], $v['openid']);
                $data['result'][$k]['num'] = $num;
                $data['result'][$k]['nickname'] = $tempUser['nickname'];
                $data['result'][$k]['headimgurl'] = $tempUser['headimgurl'];
                $data['result'][$k]['lastTime'] = date('Y-m-d H:i', $v['lastTime']);
                $num++;
            }
        }
        return $data;
    }
    
    /**
     * 获取用户微信信息
     * @param unknown $id
     * @param unknown $openid
     */
    public function getUserInfo($id, $openid)
    {
        $sql = " SELECT nickname, headimgurl FROM ".$this->table('wxuser')." WHERE id='".$id."' AND openid='".$openid."' ";
        return $this->getRow($sql);
    }
    
    /**
     * 获取记录详情
     * @param unknown $id
     * @return Ambigous <string, boolean>
     */
    public function getOneInfo($id)
    {
        $sql = " SELECT * FROM ".$this->table('gift_join')." WHERE id='".$id."' ";
        $data = $this->getRow($sql);
        if($data)
        {
            $tempUser = $this->getUserInfo($data['wxid'], $data['openid']);
            $data['nickname'] = $tempUser['nickname'];
            $data['headimgurl'] = $tempUser['headimgurl'];
        }
        return $data;
    }
    
    /**
     * 获取个人游戏记录
     * @param unknown $id
     * @param unknown $page
     * @param number $limit
     * @return Ambigous <string, multitype:string Ambigous <boolean, multitype:multitype: > >
     */
    public function getOneLog($id, $page, $limit = 20)
    {
        $sql = " SELECT * FROM ".$this->table('gift_recode')." WHERE jid='".$id."' ORDER BY created DESC ";
        $data = $this->show_page($sql, $page, $limit);
        if($data['result'])
        {
            foreach($data['result'] as $k => $v)
            {
                $data['result'][$k]['created'] = date('Y-m-d H:i', $v['created']);
                $data['result'][$k]['status'] = $v['status'] ? '有效' : '无效';
            }
        }
        return $data;
    }
    
    /**
     * 获取数量
     * @return Ambigous <string, boolean>
     */
    public function getCountJoin()
    {
        $sql = " SELECT COUNT(*) FROM ".$this->table('gift_join');
        return $this->getOne($sql);
    }
    
    public function getDaoLog($page, $limit)
    {
        $start = ($page - 1) * $limit;
        $limitSql = "";
        $num = 1;
        if($page > 0)
        {
            $num = ($page - 1) * $limit + 1;
            $limitSql = " LIMIT ".$start." , ".$limit;
        }
        $sql = " SELECT * FROM ".$this->table('gift_join')." ORDER BY allScore DESC ".$limitSql;
        $data = $this->getAll($sql);
        $arr = array();
        if($data)
        {
            foreach($data as $k => $v)
            {
                $tempUser = $this->getUserInfo($v['wxid'], $v['openid']);
                $arr[] = array(
                    'num' => $num,
                    'nickname' => $tempUser['nickname'],
                    'openid' => $v['openid'],
                    'allScore' => $v['allScore'],
                    'allCount' => $v['allCount'],
                    'gameScore' => $v['gameScore'],
                    'gameCount' => $v['gameCount'],
                    'shareScore' => $v['shareScore'],
                    'shareCount' => $v['shareCount'],
                    'addScore' => $v['addScore'],
                    'noScore' => $v['noScore'],
                    'lastTime' => date('Y-m-d H:i', $v['lastTime']),
                    'lastIp' => $v['lastIp']
                );
            }
        }
        return $arr;
    }
    
    /**
     * 加分记录
     * @param unknown $wxid
     * @param unknown $openid
     * @return Ambigous <boolean, multitype:multitype: >
     */
    public function getAddLog($wxid, $openid, $page, $limit = 20)
    {
        $sql = " SELECT * FROM ".$this->table('gift_recode')." WHERE shareWxid='".$wxid."' AND shareOpenid='".$openid."' ORDER BY created DESC ";
        $data = $this->show_page($sql, $page, $limit);
        if($data['result'])
        {
            foreach($data['result'] as $k => $v)
            {
                $userTemp = $this->getUserInfo($v['wxid'], $v['openid']);
                $data['result'][$k]['nickname'] = $userTemp['nickname'];
                $data['result'][$k]['headimgurl'] = $userTemp['headimgurl'];
                $data['result'][$k]['created'] = date('Y-m-d H:i', $v['created']);
            }
        }
        return $data;
    }
    
}